
package com.kweative.lingua.server;

import com.kweative.lingua.server.protocol.Protocol;
import com.kweative.lingua.server.request.RequestHandler;
import com.kweative.lingua.server.structures.NamedPool;
import com.kweative.network.Request;
import java.util.Collection;
import java.util.HashMap;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author henrik
 */
public class ProtocolPool extends NamedPool<String,Protocol> {
    private static ProtocolPool instance = null;
    
	public static ProtocolPool instance() {
		if (instance == null)
			instance = new ProtocolPool();
		return instance;
	}

    private Protocol defaultProtocol = null;
    
    public void add(Protocol p) {
        defaultProtocol = p;
        add(p.getClass().getSimpleName().toLowerCase(), p);
    }

    public Collection<Protocol> protocols() {
        return getPool().values();
    }

    public Protocol getRequestProtocol(RequestHandler r) {
        for(Protocol p:getPool().values()) {
            try {
                if (p.canRead(r))
                    return p;
            } catch(Throwable ex) {
                Logger.getLogger(ProtocolPool.class.getName()).log(Level.SEVERE,"A protocol error occured for:" + p.toString(),ex);
            }
        }
        return null;
    }
    public Protocol getProtocol(String name) {
        return get(name);
    }
    public Protocol getDefaultProtocol() {
        return defaultProtocol;
    }
}
